#include "client.h"

int tcp_cnd_init(const char *ip, int port)
{

    int cnd = socket(PF_INET, SOCK_STREAM, 0);
    ERROR_CHECK_FUN(-1, cnd,"socket");
    struct sockaddr_in caddr;
    caddr.sin_family = PF_INET;
    caddr.sin_port = htons(port);
    caddr.sin_addr.s_addr = inet_addr(ip);
    int ret = connect(cnd, (void *)&caddr, sizeof caddr);
    ERROR_CHECK_FUN(-1, ret,"connect");
    return cnd;
}
int userSys(int net_fd)
{
    char name[32];
    char passwd[32];
    char buf[1024];
    while (1)
    {

        system("clear");
        printf("****************************************\n");
        printf("********欢迎使用分布式任务处理系统********\n");
        printf("*1、登录 2、注册 3、注销 4、修改 任意键退出 *\n");
        printf("****************************************\n");
        int cmd_user = 0;
        scanf("%d", &cmd_user);
        getchar();
        switch (cmd_user)
        {
        case 1:
            bzero(buf,32);
            sprintf(buf, "%s", USER_LOG);
            send(net_fd, buf, 32, 0);
//
            printf("please input usr name: ");
            fgets(name,32,stdin);
            printf("please input usr password: ");
            fgets(passwd,32,stdin);
            snprintf(buf,1024,"%s%s",name,passwd);
            send(net_fd, buf, 1024, 0);
            bzero(name,32);
            puts(buf);
            recv(net_fd, name, 32, 0);
            puts(name);
            sleep(1);
            if (0 == strcmp(name, LOG_SUCCESS))
            {
                puts("登录成功");
                
                return 0;
            }
            if (0 == strcmp(name, LOG_FAILED))
            {
                puts("登陆失败,账号或密码错误!");
                sleep(1);
            }
            break;
        case 2:
            bzero(buf,32);
            sprintf(buf, "%s", USER_REGISTER);
            send(net_fd, buf, 32, 0);
//
            printf("please input usr name: ");
            fgets(name,32,stdin);
            printf("please input usr password: ");
            fgets(passwd,32,stdin);
            snprintf(buf,1024,"%s%s",name,passwd);
            
            send(net_fd, buf, 1024, 0);
            bzero(name,32);
            puts(buf);
            recv(net_fd, name, 32, 0);
            puts(name);
            sleep(1);
            if (0 == strcmp(name, REG_SUCCESS))
            {
                puts("注册成功");
                sleep(1);
            }
            if (0 == strcmp(name, REG_FAILED))
            {
                puts("注册失败,重新注册!");
                sleep(1);
            }
            break;
        case 3:

            break;
        case 4:

            break;
        default:
            puts("退出");
            exit(1);
            break;
        }
    }
}
int epollAdd(int fd, int epfd){
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = fd;
    int ret = epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
    ERROR_CHECK(ret,-1,"epoll_ctl");
    return 0;
}int epollDel(int fd, int epfd){
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = fd;
    int ret = epoll_ctl(epfd,EPOLL_CTL_DEL,fd,&event);
    ERROR_CHECK(ret,-1,"epoll_ctl");
    return 0;
}
